<?php
class Z1thongbaosController extends AppController {

	var $name = 'Z1thongbaos';
	var $uses = array('Z1thongbao','Thongbao');
	function index($nhanvien_id=null) {
		$this->Z1thongbao->recursive = 0;
		$this->set('z1thongbaos', $this->paginate(array('nhanvien_id'=>$nhanvien_id)));
        $this->set(compact('nhanvien_id'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid z1thongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('z1thongbao', $this->Z1thongbao->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Z1thongbao->create();
			if ($this->Z1thongbao->save($this->data)) {
				$this->Session->setFlash(__('The z1thongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1thongbao could not be saved. Please, try again.', true));
			}
		}
		$nhanviens = $this->Z1thongbao->Nhanvien->find('list');
		$chucdanhs = $this->Z1thongbao->Chucdanh->find('list');
		$this->set(compact('nhanviens', 'chucdanhs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid z1thongbao', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Z1thongbao->save($this->data)) {
				$this->Session->setFlash(__('The z1thongbao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The z1thongbao could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Z1thongbao->read(null, $id);
		}
		$nhanviens = $this->Z1thongbao->Nhanvien->find('list');
		$chucdanhs = $this->Z1thongbao->Chucdanh->find('list');
		$this->set(compact('nhanviens', 'chucdanhs'));
	}

	function delete($nhanvien_id=null,$id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Thông báo không tồn tại', true));
			$this->redirect(array('action'=>'index'));
		}
		$z1tb  =$this->Z1thongbao->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$id)));
		if(!empty($z1tb))
		{
			$tbs = split(',',$z1tb['Z1thongbao']['group_id']);
			foreach ($tbs as $value)
			{
				if(!$this->Thongbao->delete($value))
				{
					$this->Session->setFlash(__('Không thể xóa. xin vui lòng thử lại', true));
				}
			}
			/*if ($this->Z1thongbao->delete($id)) {
				$this->Session->setFlash(__('Z1thongbao deleted', true));
				$this->redirect(array('action'=>'index'));
			}*/
		}
		$this->Session->setFlash(__('Đã xóa Thông báo', true));
		$this->redirect(array('action' => 'index',$nhanvien_id));
	}

}
